﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc;
using Kendo.Mvc.Infrastructure;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using SecurityApi.DataAccess;
using SecurityApi.Business;

namespace SecurityAdmin.Web.Controllers
{
    public class ApplicationController : BaseController
    {
        //
        // GET: /Application/

        public ActionResult GetApplications()
        {
            return View();
        }

        public ActionResult ApplicationSelected()
        {
            return View();
        }

        public ActionResult AddNewApplication()
        {
            Application newApplication = new Application();
            return View(newApplication);
        }

        [HttpPost]
        public ActionResult AddNewApplication(Application newApp)
        {
            ApplicationRepository repository = new ApplicationRepository();
            int roleId = repository.AddApplication(newApp.Name,EnterpriseId); 
            return RedirectToAction("GetApplications");
        }

        public ActionResult RemoveApplication(int appId)
        {
            ApplicationRepository repository = new ApplicationRepository();
            bool bResult = repository.RemoveApplication(appId);
            return RedirectToAction("GetApplications");
        }

        public ActionResult SelectApplication(int appId)
        {
            ApplicationManager mgr = new ApplicationManager();
            SecurityApi.Model.Application app = mgr.GetApplicationById(appId);
            SetCurrentApplication(app);
            return RedirectToAction("ApplicationSelected");
        }

        public ActionResult AjaxGetAllApplications([DataSourceRequest] DataSourceRequest request)
        {
            ApplicationManager mgr = new ApplicationManager();
            IQueryable<SecurityApi.Model.Application> enterpriseApps = mgr.GetAllApplications();
            return Json(enterpriseApps.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }

        public ActionResult AjaxEnterpriseApplications(long enterpriseId, [DataSourceRequest] DataSourceRequest request)
        {
            ApplicationManager mgr = new ApplicationManager();
            List<SecurityApi.Model.Application> enterpriseApps = mgr.GetEnterpriseApplications(enterpriseId);
            return Json(enterpriseApps.ToDataSourceResult(request), JsonRequestBehavior.AllowGet);
        }


        public ActionResult ShowApplicationUsers()
        {
            return View();
        }
    }
}
